import React, { useEffect, useState } from 'react'
import { Card, Table, Button, message } from 'antd';
import {  delone } from "../../../http/management.js"
import { useDispatch,useSelector } from "umi"
import UPdate from "../../../components/managementUseradd"

export default function index() {
    const dispatch = useDispatch()
    const columns = [
        {
            title: '真实姓名',
            dataIndex: 'realname',
            key: 'realname'
        },
        {
            title: '用户',
            dataIndex: 'account',
            key: 'account',
        },
        {
            title: '密码',
            dataIndex: 'password',
            key: 'password',
        },
        {
            title: '电话',
            key: 'telephone',
            dataIndex: 'telephone'
        },
        {
            title: '操作',
            key: 'action',
            render: (text) => (
                <Button onClick={() => del(text)}>删除</Button>
            ),
        },
    ];
    const data=useSelector((state)=>{
        return state.maModels.alldata
    })
    useEffect(() => {
        getuserData()
    }, [])


    //获取用户数据
    const getuserData = async () => {
        
        dispatch({
            type:'maModels/getalluser'
        })
    }

    //删除用户
    const del = async (val) => {
        const res = await delone({
            _id: val._id
        })
        if (res.code == 200) {
            message.success('删除成功')
            getuserData();
        } else {
            message.error('删除失败')
        }
    }
    //新增
    const add = () => {
        dispatch({
            type: 'maModels/changestatus',
            payload: true
        })
    }
    return (
        <>
            <Card title="管理员列表" extra={<a onClick={add}>新增</a>} style={{ width: "98.5%" }}>
                <Table columns={columns} rowKey="_id" dataSource={data} pagination={{ defaultCurrent: 1, defaultPageSize: 5, showQuickJumper: true }} />
            </Card>
            <UPdate getdata={getuserData}></UPdate>
        </>
    )
}
